//.导入Vue类  导入路由类
import Vue from "vue"
import VueRouter from "vue-router"
//2.导入组件
import Login from "@/components/Login.vue"
import Home from "@/components/Home.vue"

//3.全局注册
Vue.use(VueRouter)

//4.创建 路由规则
const routes = [
    {
        path: '/',
        redirect: '/login'
    },
    {
        path: '/login',
        component: Login
    },
    {
        path: '/home',
        component: Home
    },
]
//创建 路由管理器对象
const router = new VueRouter({ routes })
//添加  路由守卫
router.beforeEach((to, from, next) => {
    //a.如果访问/login,直接放行
    if (to.path === "/login") return next()
    //b.如果不访问/login 则检查sessionStorage中是否有token
    const token = window.sessionStorage.getItem('token')
    if (!token) return next("/login")
    //c.如果访问非/login ，并且有token  则放行
    next(true)


})
//4.创建并 导出路由管理对象
export default router